package com.gitee.demo.param.common;

import org.apache.maven.shared.utils.StringUtils;

import java.io.Serializable;

/**
 * @author Tao on 2020/7/29
 */
public class Result<T> implements Serializable {
    private String code;
    private String msg;
    private T data;

    private Result() {
        this.code = QuanMinErrorCode.SUCCESS.getCode();
        this.msg = QuanMinErrorCode.SUCCESS.getMsg();
    }

    private Result(T data) {
        this.code = QuanMinErrorCode.SUCCESS.getCode();
        this.msg = QuanMinErrorCode.SUCCESS.getMsg();
        this.setData(data);
    }

    private Result(String code, String msg) {
        this.code = QuanMinErrorCode.SUCCESS.getCode();
        this.msg = QuanMinErrorCode.SUCCESS.getMsg();
        this.setCode(code);
        this.setMsg(msg);
    }

    private Result(String code, String msg, T data) {
        this.code = QuanMinErrorCode.SUCCESS.getCode();
        this.msg = QuanMinErrorCode.SUCCESS.getMsg();
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    private Result(QuanMinErrorCode dsqErrorCode) {
        this.code = dsqErrorCode.getCode();
        this.msg = dsqErrorCode.getMsg();
        this.setCode(code);
        this.setMsg(msg);
    }

    public Result<T> setError(String code, String msg) {
        this.setCode(code);
        this.setMsg(msg);
        return this;
    }

    public Result<T> setError(QuanMinErrorCode errorCode) {
        this.setCode(errorCode.getCode());
        this.setMsg(errorCode.getMsg());
        return this;
    }

//    public boolean isSuccess() {
//        return StringUtils.equals(this.getCode(), QuanMinErrorCode.SUCCESS.getCode());
//    }

    public static <T> Result instance() {
        return new Result();
    }
    public static <T> Result instance(QuanMinErrorCode errorCode) {
        return new Result(errorCode);
    }
    public static <T> Result instance(T data) {
        return new Result(data);
    }

    public static <T> Result instance(String code, String msg) {
        return new Result(code, msg);
    }

    public static <T> Result instance(String code, String msg, T data) {
        return new Result(code, msg, data);
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
